package org.centsuse.intention_spring.invention.segmentation.clients;

import lombok.extern.slf4j.Slf4j;
import org.centsuse.intention_spring.invention.config.models.ModelConfig;
import org.centsuse.intention_spring.invention.config.models.ModelConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Map;

@Configuration
@Slf4j
public class WebClientConfig {

//    @Value("${deepseek.api.base-url}")
//    private String baseUrl;
//
//    @Value("${deepseek.api.api-key}")
//    private String apiKey;
    /** 模型配置 */
    private final ModelConfiguration modelConfiguration;

    public WebClientConfig(ModelConfiguration modelConfiguration) {
        this.modelConfiguration = modelConfiguration;
    }

    @Bean
    @Nullable
    public WebClient webClient() {
        Map<String, ModelConfig> modelConfigs = modelConfiguration.getModelConfigMap();
        ModelConfig modelConfig = modelConfigs.get("deepseek-r1");
        log.info("Creating web client: {}", modelConfig);
        if (null == modelConfig) {
            return null;
        }
        return WebClient.builder()
                .baseUrl(modelConfig.getBaseUrl())
                .defaultHeader("Authorization", "Bearer " + modelConfig.getApiKey())
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .build();
    }
}
